<?php

require_once 'adodb5/adodb-errorhandler.inc.php';
require_once 'adodb5/adodb.inc.php';

class Sync_Mongo_Mysql {

    private $_conn_adodb, $_conn_mongo, $_db_mongo, $_find, $_count = 1;

    /**
     * 
     * @param type $dbtype
     * @param type $host
     * @param type $username
     * @param type $pass
     * @param type $dbname
     * 
     */
    public function __construct($host = "", $username = "", $pass = "", $dbname = "", $dbtype = "mysql") {
        try {
            $this->_conn_adodb = &ADONewConnection($dbtype);
            $this->_conn_adodb->Connect($host, $username, $pass, $dbname);
            $this->_conn_adodb->execute("set names utf8");
            $this->_conn_mongo = new MongoClient();
            $this->_db_mongo = $this->_conn_mongo->selectDB($dbname);
        } catch (Exception $exc) {
            die("Connection MongoDB Fail...");
        }
    }

    /**
     * 
     * @param array $values
     * @param type $table
     * 
     */
    public function insert($table, array $values) {
        $query = "INSERT INTO $table SET ";
        $count = count($values);
        foreach ($values as $key => $value) {
            if ($this->_count == $count)
                $query .= "$key='{$value}' ";
            else
                $query .= "$key='{$value}', ";
            $this->_count++;
        }
        try {
            $this->_conn_adodb->execute($query);
            $id = $this->_conn_adodb->Insert_Id();
            $values = $values + array("_id" => $id);
            $this->_db_mongo->selectCollection($table)->save($values);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }

    /**
     * 
     * @param type $table
     * @param array $values
     * @param type $id
     * 
     */
    public function update($table, array $values, $id) {
        $query = "UPDATE $table SET ";
        $count = count($values);
        foreach ($values as $key => $value) {
            if ($this->_count == $count)
                $query .= "$key='{$value}' ";
            else
                $query .= "$key='{$value}', ";
            $this->_count++;
        }
        $query .= "WHERE id='{$id}'";
        try {
            $this->_conn_adodb->execute($query);
            $values = $values + array("_id" => $id);
            $this->_db_mongo->selectCollection($table)->save($values);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }

    /**
     * 
     * @param type $table
     * @param type $option_id
     * 
     */
    public function delete($table, $option_id) {
        $id = array("_id" => $option_id);
        $query = "DELETE FROM $table FROM id='{$option_id}'";
        try {
            $this->_conn_adodb->execute($query);
            $this->_db_mongo->selectCollection($table)->remove($id);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }
    /**
     * 
     * @param type $table
     * @param array $query
     * @param array $fields
     * @return type
     * 
     */
    public function find($table, array $query = array(), array $fields = array()) {
        $cursor =  $this->_db_mongo->selectCollection($table)->find($query, $fields);
//        echo $cursor;
//        foreach ($cursor as $_xx){
//            var_export($_xx);die;
//        }
        return new Cursor($cursor);
//        return $this;
//        return new Cursor(); đấy chỗ này nếu e return nó về class thì bắt dc nhưng function. Nhưng nếu nhu thế thì
//        nó sẽ ko trả về giá trị cho e dc. Nên khi qua thằng index.php nó call ra giá trị trả về null
    }
}

class Cursor{
    
    private $_data;
    
    public function __construct($data) {
        $this->_data = $data;
//        foreach ($data as $_data){
//            var_export($_data);die;
//        }
        return $this->_data;
    }
    
    public function find(){
        return $this->_data;
    }
           
    
    public function limit($number){
        
    }
    
    public function sort(array $field){
        
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
